package se.zust.sshjpa.controller;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import se.zust.sshjpa.dto.ContactsDto;
import se.zust.sshjpa.service.ContactsServiceI;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/contacts")
@Api(tags="ContactsController",description="联系人管理")
public class ContactsController {
    private static Logger logger=Logger.getLogger(ContactsController.class);
    @Resource
    ContactsServiceI contactsService;
    @RequestMapping("/jsearch")
    @ResponseBody
    @ApiOperation(httpMethod="GET",value="查询联系人信息",notes="根据联系人姓名模糊查询")
    public List<ContactsDto> searchContactsJson(String name){
        ModelAndView mav=new ModelAndView("contacts");
        if(name==null||name.trim().length()==0)
            name="";
        List<ContactsDto> contactsDtoList= contactsService.findByName("%"+name+"%");
        return contactsDtoList;
    }
    @RequestMapping("/search")
    public ModelAndView searchContacts(String name){
        ModelAndView mav=new ModelAndView("contacts");
        if(name==null||name.trim().length()==0)
            name="";
        List<ContactsDto> contactsDtoList= contactsService.findByName("%"+name+"%");
        mav.addObject("contactsList",contactsDtoList);
        mav.addObject("name",name);
        return mav;
    }

    @RequestMapping(value = "/save",method = RequestMethod.GET)
    public String saveContacts(ContactsDto dto){
        return "addContacts";
    }
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ModelAndView doSaveContacts(ContactsDto dto){
        ModelAndView mav=new ModelAndView("redirect:/contacts/search.html");
        contactsService.saveContacts(dto);
        return mav;
    }
}
